package com.learn.xml.util;

import org.dom4j.Document;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class XmlUtil {

    private final static Logger log = LoggerFactory.getLogger(XmlUtil.class);

    public static void main(String[] args) {
        new XmlUtil().updateConfigurationXml("softwareName","你好");
    }

    public void updateConfigurationXml(String key, String value) {
        InputStream inputStream = null;

        try {
            String relativePath = File.separator + "conf" + File.separator + "configuration.xml";

            String currentDirectory = System.getProperty("user.dir");

            String path = this.getClass().getClassLoader().getResource("").getPath();
            System.out.println(path);
            String outFilePath = currentDirectory + File.separator + relativePath;

            //String outFilePath="E:/AWorkPlace-IDE/util-learn/util/src/main/java/conf/configuration.xml";

            inputStream = this.getClass().getResourceAsStream("/conf/configuration.xml");

            updateXml(inputStream, path+"conf/configuration.xml", "configuration", "property", "item", key, "value", value);

        } catch (Exception e) {
            log.error("更新输出目录异常：" + e.getMessage());
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    log.error("更新输出目录关闭IO流异常：" + e.getMessage());
                }
            }

        }

    }

    public static void updateXml(InputStream inputStream, String outFilePath, String conf, String prop
            , String keyName, String key, String valueName, String value) throws Exception {

        Document document = (new SAXReader()).read(inputStream);
        Element rootElement = document.getRootElement();
        boolean flag = false;
        Iterator iterator = rootElement.elementIterator();

        Element element;
        while(iterator.hasNext()) {
            element = (Element) iterator.next();
            String str = element.attributeValue(keyName);
            element.attributeValue(valueName);
            if (str.equals(key)) {
                element.addAttribute(valueName, value);
                flag = true;
            }
        }

        if (flag) {
            iterator = null;
            element = null;
            XMLWriter xmlWriter = null;

            try {
                FileOutputStream fileOutputStream = new FileOutputStream(outFilePath);
                OutputFormat outputFormat = OutputFormat.createPrettyPrint();
                outputFormat.setEncoding("utf-8");
                xmlWriter = new XMLWriter(fileOutputStream, outputFormat);
                xmlWriter.write(document);
            } catch (Exception e) {
                log.error(e.getMessage());
            } finally {
                if (xmlWriter != null) {
                    xmlWriter.close();
                }

            }

        }
    }




}
